﻿//********************************************************************************//
//创建日期：<创建日期，2017-10-13>
//创建作者：<郭登杰，guodengjie@shanghai3h.com>
//功能说明：支票现金付委 自来水
//********************************************************************************//
layui.use(['form', 'jquery', 'element', 'laydate'], function () {
    var form = layui.form()
    , layer = layui.layer
    , $ = layui.jquery
    , element = layui.element()
    , laydate = layui.laydate;
    /*initial load start*/
    initcontrol();
    load3();
    /*initial load end*/

    /*initial*/
    function initcontrol() {
        //设置默认打印日期与回执日期
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        $("#c2DYRQ").val(year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day));
        $("#c2SFRQ").val(year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day));        
        //获取当前账务年月
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/Common.ashx",
            data: {
                "op": "YSZWYM"
            },
            dataType: "json",
            success: function (rs) {
                $("#oiSFRQ").val(rs.Data1);
                //$("#c2ZWYM").val(rs.Data1);
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });

    }
    /*end initial*/

    /*event*/
    $("#cYHH").keydown(function () {
        if (event.keyCode == 13 && $(this).val() != "") {
            var index = layer.load(1);
            //加载欠费信息合计(应收金额,实收金额)
            if (loadQFXX_HJ()) {
                //清空本次收费
                $("#ContentList2 .datarow").remove();
                //加载欠费信息明细(水量年月,应收,已收,欠费)
                loadQFMX();
                layer.close(index);
            }
            else {
                layer.close(index);
                layer.alert("未找到该用户.");
                return;
            }

        }
    });
    function loadQFXX_HJ() {
        var success = true;
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSSF/ZhangDan.ashx",
            data: {
                "op": "QFXX_HJ",
                "yhh": $("#cYHH").val()
            },
            dataType: "json",
            success: function (rs) {
                success = rs.State;
                if (success) {
                    $("#ciYHM").val(rs.Data1["S_YONGHUM"]);
                    $("#ciYSJE").val(rs.Data1["N_YINGSHOUJE"].toFixed(2));
                    $("#ciSSJE").val(rs.Data1["N_SHISHOUJE"].toFixed(2));
                    $("#ciQFJE").val(rs.Data1["N_QIANFEIJE"].toFixed(2));
                }
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        return success;
    }
    function loadQFMX() {
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSSF/ZhangDan.ashx",
            data: {
                "op": "QFXX_MX",
                "yhh": $("#cYHH").val()
            },
            dataType: "json",
            success: function (rs) {
                var strs = "";
                var jesum = 0;
                for (var i in rs) {
                    strs = strs + createItemStr(rs[i]);
                    jesum = jesum + rs[i]["N_QIANFEIJE"];
                }
                $("#ContentList1").find(".datarow").remove();
                $("#ContentList1").append(strs);
                //制作合计行
                strs = "<tr class='datarow' vid='hj' style='cursor:pointer'><td colspan='4'>" + "欠费记录:" + rs.length + "条,合计:" + jesum.toFixed(2) + "元" + "</td></tr>";
                $("#ContentList1").append(strs);
                //绑定点击后加自动添加到左侧收费列表
                list1bind();
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
    }
    function list1bind() {
        $("#ContentList1 .datarow").click(function () {
            var trs;
            if ($(this).attr("vid") == "hj") {
                trs = $("#ContentList1 .datarow").not($(this));
            }
            else {
                trs = $(this);
            }
            //记录已添加的项
            var addkeys = new Array();
            var trs2 = $("#ContentList2 .datarow");
            for (var i = 0; i < trs2.length; i++) {
                if ($(trs2).attr("vid") != "hj") {
                    addkeys.push($(trs2[i]).attr("vid"));
                }
            }
            //移除收费列表
            $("#ContentList2 .datarow").remove();
            //重新添加
            for (var i = 0; i < trs.length; i++) {
                //判断是否已经添加
                var exists = false;
                for (var j = 0; j < addkeys.length; j++) {
                    if ($(trs[i]).attr("vid") == addkeys[j]) {
                        exists = true;
                        break;
                    }
                }
                if (exists == false) {
                    addkeys.push($(trs[i]).attr("vid"));
                }
            }
            var strTemp = "";
            var source = $("#ContentList1 .datarow").not("[vid='hj']");
            for (var i = 0; i < $(source).length; i++) {
                for (var j = 0; j < addkeys.length; j++) {
                    if ($(source[i]).attr("vid") == addkeys[j]) {
                        strTemp = "<tr class='datarow' style='cursor:pointer' vid='" + $(source[i]).attr("vid") + "'><td style='text-align:center'>"
                            + $(source[i]).find("td").eq(0).text() +
                        "</td>\<td style='text-align:right'>" + $(source[i]).find("td").eq(3).text() + "</td><td style='text-align:right'>"
                        + $(source[i]).find("td").eq(3).text() + "</td></tr>";
                        $("#ContentList2").append(strTemp);
                        break;
                    }
                }
            }
            //插收收费合计
            trs2 = $("#ContentList2 .datarow");
            var jesumT = 0;
            for (var i = 0; i < trs2.length; i++) {
                jesumT = jesumT + parseFloat($(trs2[i]).find("td").eq("2").text());
            }
            var strHJ = "<tr class='datarow' vid='hj' style='cursor:pointer'><td colspan='3'>" + "本次收费:" + trs2.length + "条,合计:" + jesumT.toFixed(2) + "元" + "</td></tr>";;

            $("#ContentList2").append(strHJ);

            //绑定左侧移除效果
            list2bind();
        });
    }
    function list2bind() {
        $("#ContentList2 .datarow").click(function () {

            var trs;
            if ($(this).attr("vid") == "hj") {
                trs = $("#ContentList2 .datarow");
            }
            else {
                trs = $(this);
            }
            $(trs).remove();
            //移除合计
            $("#ContentList2 .datarow[vid='hj']").remove();
            //插收收费合计
            if ($("#ContentList2 .datarow").length > 0) {
                trs2 = $("#ContentList2 .datarow");
                var jesumT = 0;
                for (var i = 0; i < trs2.length; i++) {
                    jesumT = jesumT + parseFloat($(trs2[i]).find("td").eq("2").text());
                }
                var strHJ = "<tr class='datarow' vid='hj' style='cursor:pointer'><td colspan='3'>" + "本次收费:" + trs2.length + "条,合计:" + jesumT.toFixed(2) + "元" + "</td></tr>";;

                $("#ContentList2").append(strHJ);
            }
        });
    }
    function createItemStr(obj) {
        var str = $("#ItemStr1 tr").prop("outerHTML");
        var itemstr, zdbh, ym, yhh, yhm, ysje, ssje, qfje;
        zdbh = obj["S_ZHANGDANBH"];
        ym = obj["I_YM"];
        yhh = obj["S_YONGHUH"];
        yhm = obj["S_YONGHUM"];
        ysje = obj["N_YINGSHOUJE"];
        ssje = obj["N_SHISHOUJE"];
        qfje = obj["N_QIANFEIJE"];
        itemstr = str.replace(new RegExp("objZDBH", "gm"), zdbh)
                        .replace(new RegExp("objYM", "gm"), ym)
                        .replace(new RegExp("objYSJE", "gm"), ysje.toFixed(2))
                        .replace(new RegExp("objSSJE", "gm"), ssje.toFixed(2))
                        .replace(new RegExp("objQFJE", "gm"), qfje.toFixed(2))
        ;
        return itemstr;
    }

    $("#oSF").click(function () {
        if ($("#ContentList2 .datarow").length == 0) {
            layer.alert("未选择收费记录");
            return;
        }
        if ($("#oiSFFS").val() == "0") {
            layer.alert("请选择收费方式");
            return;
        }
        var index = layer.load(1);
        var success = true;
        var sfbh = -1;
        //先获取当前收费编号
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSSF/ZhangDan.ashx",
            data: {
                "op": "SFBHHQ"
            },
            dataType: "text",
            success: function (rs) {
                //新编号
                sfbh = parseInt(rs)+1;
            },
            error: function (error) {                
                layer.msg(error.responseText);
            }
        });
        $("#ContentList2 .datarow").not("[vid='hj']").each(function () {
            var zdbh = $(this).attr("vid");
            var sfje = $(this).find("td").eq(2).text();
            var sfrq = $("#oiSFRQ").val();
            var type = $("#oiSFFS").val();
            
            $.ajax({
                type: "post",
                cache: false,
                async: false,
                url: "/ashx/YSSF/ZhangDan.ashx",
                data: {
                    "op": "SF_ZPXJFW",
                    "sfbh": sfbh,
                    "zdbh": zdbh,
                    "sfje": sfje,
                    "sfrq": sfrq,
                    "type": type
                },
                dataType: "json",
                success: function (rs) {
                    if (!rs.State) {
                        success = false;
                    }
                },
                error: function (error) {
                    success = false;
                    layer.msg(error.responseText);
                }
            });
        });
        if (success) {
            //清除当前收费列表及搜索条件信息
            $("#ContentList2 .datarow").remove();
            $("#ContentList1 .datarow").remove();            
            $("#cYHH").val("");
            $("#ciYHM").val("");
            $("#ciYSJE").val("");
            $("#ciSSJE").val("");
            $("#ciQFJE").val("");
            //loadQFXX_HJ();
            //loadQFMX();
            load3();
            $("#cYHH").focus();
            $("#cYHH").select();
        }
        layer.close(index);
    });
    $("#oView").click(function () {
        layer.open({
            type: 2,
            title: "",
            shadeClose: false,
            shade: 0.8,
            area: ['900px', '590px'],
            content: "/Pages/YongShui/5.ShouFeiGL/ZhiPiaoXJFW_CK.aspx?ym=" + encodeURI($("#oiSFRQ").val())
        });
    });

    function load3() {
        var zwym = $("#c2ZWYM").val();
        var sfrq = $("#c2SFRQ").val();
        var yhh = $("#c2YHH").val();
        var rss = "";
        $.ajax({
            type: "post",
            cache: false,
            async: false,
            url: "/ashx/YSSF/ZhangDan.ashx",
            data: {
                "op": "SFXX_MX",
                "zwym": zwym,
                "sfrq": sfrq,
                "yhh": yhh
            },
            dataType: "json",
            success: function (rs) {
                rss = rs;
            },
            error: function (error) {
                layer.msg(error.responseText);
            }
        });
        var strs = "";
        $("#ContentList3").find(".datarow").remove();
        if (rss.length > 0) {
            chunk(rss, list3Create, list3OpreateBind);
        }
        else {
            $("#ls3Count").text("0");
            $("#ls3JESum").text("0.00");
        }
    }
    $("#o2Search").click(function () {
        if ($("#c2ZWYM").val() == "" && $("#c2SFRQ").val() == ""&&$("#c2YHH").val() == "") {
            layer.alert("未避免提取数据过多,请至少选择输入一个条件");
            return;
        }        
        load3();
    });
    $("#c2ZWYM,#c2YHH").keydown(function () {
        if (event.keyCode == 13) {
            load3();
        }
    });
    //laydate({ choose: load3 });
    function list3Create(rs, i) {
        $("#ContentList3").append(createItemStr3(rs));
        $("#ls3Count").text($("#ContentList3 .datarow").length);
        var jeSum = 0;
        $("#ContentList3 .datarow").each(function () {
            jeSum = jeSum + parseFloat($(this).find("td").eq("5").text());
        });
        $("#ls3JESum").text(jeSum.toFixed(2));
    }
    function createItemStr3(obj) {
        var str = $("#ItemStr3 tr").prop("outerHTML");
        var itemstr, zdbh, ym, yhh, yhm, sfje, sfrq, sffs, zwym, id;
        id=obj["ID"];
        zdbh = obj["S_ZHANGDANBH"];
        ym = obj["I_YM"];
        yhh = obj["S_YONGHUH"];
        yhm = obj["S_YONGHUM"];
        sfje = obj["N_SHOUFEIJE"];
        sffs = obj["S_SHOUFEIFS"];
        zwym = obj["I_ZWYM"];
        sfrq = obj["S_SHOUFEIRQ"];
        itemstr = str.replace(new RegExp("objZDBH", "gm"), zdbh)
                        .replace(new RegExp("objSFRQ", "gm"), sfrq)
                        .replace(new RegExp("objYHH", "gm"), yhh)
                        .replace(new RegExp("objYHM", "gm"), yhm)
                        .replace(new RegExp("objYM", "gm"), ym)
                        .replace(new RegExp("objSFJE", "gm"), sfje.toFixed(2))
                        .replace(new RegExp("objSFFS", "gm"), sffs)
                        .replace(new RegExp("objZWYM", "gm"), zwym)
                        .replace(new RegExp("objID", "gm"), id)
        ;
        return itemstr;
    }
    function list3OpreateBind() {
        form.render("checkbox");
        //行点击切换选中状态
        $("#ContentList3 .datarow").each(function () {
            var cb = $(this).find("[name='liCheck']");
            $(this).find("td:gt(0)").unbind("click").click(function () {
                if ($(cb).prop("checked")) {
                    $(cb).removeProp("checked");
                }
                else {
                    $(cb).prop("checked", "checked");
                }
                form.render("checkbox");
            });
        });
    }
    //临听全选/取消
    form.on("checkbox(selectAll3)", function (data) {
        if (data.elem.checked) {
            $("#ContentList3 .datarow:visible").find("[name='liCheck']").prop("checked", "checked");
        }
        else {
            $("#ContentList3 .datarow:visible").find("[name='liCheck']").removeProp("checked");
        }
        form.render("checkbox");
    });

    $("#o2SFQX").click(function () {
        var checks = $("#ContentList3 .datarow").find("[name='liCheck']:checked");
        if ($(checks).length == 0) {
            layer.alert("请选择要取消的收费");
            return;
        }
        layer.confirm('确定要对选择的收费进行冲账吗?', function () {
            var success = true;            
            var ids = new Array();
            var zdbhs = new Array();
            $(checks).each(function () {
                ids.push($(this).attr("vid2"));
                zdbhs.push($(this).attr("vid"));                
            });
            $.ajax({
                type: "post",
                cache: false,
                async: false,
                url: "/ashx/YSSF/ZhangDan.ashx",
                data: {
                    "op": "SFXX_QX",
                    "ids":ids.join(","),
                    "zdbhs": zdbhs.join(",")                    
                },
                dataType: "json",
                success: function (rs) {
                    if (rs.State) {
                        layer.alert("冲账成功");
                    }
                },
                error: function (error) {
                    success = false;
                    layer.msg(error.responseText);
                }
            });
            if (success) {
                //重新加载
                load3();
            }
        });
    });
    /*event end*/

    function verify() {
        return true;
    }

    /*logic end*/
});